package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.utils.date.DateHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 区服 userid 角色创建时间 财神密卷充值时间 财神密卷期数 最后登录时间 最后充值时间
 * @Author xx
 * @Date 2020/11/11
 */
public class GetSecretVolume extends BaseRepair {

    public GetSecretVolume(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void go(){
        List<Map<String, String>> dataList = getIsBuy("2020-01-24","2022-11-12");
        dataList = getRechargeOrder(dataList);
        DataMgr.addData(dataList);
    }

    private List<Map<String, String>> getIsBuy(String dateStart,String dateEnd) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select userId,`no`,startDate,dateLength,isBuy from t_u_secret_volume WHERE isBuy = 1 AND updateTime > ? AND updateTime < ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i = 1;
            call.setString(i++,dateStart);
            call.setString(i++,dateEnd);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("期数",checkNullToEmpty(rs.getString("no")));
                temp.put("startDate",checkNullToEmpty(rs.getString("startDate")));
                temp.put("dateLength",checkNullToEmpty(rs.getString("dateLength")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
            return dataList;
        }
    }

    private List<Map<String, String>> getRechargeOrder(List<Map<String, String>> idataList) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_recharge_detail " +
                " WHERE mallId = 270101018 AND (orderStatus = 1 or orderStatus = 2) AND userId = ? AND `createTime` > ? AND `createTime` < ?" +
                " ORDER BY createTime ASC LIMIT 1";
        try {
            for(Map<String,String> one : idataList){
                long startTimeStamp = DateHelper.str2timestamp(one.get("startDate") + " 00:00:00");
                long day = Integer.valueOf(one.get("dateLength"));
                long endTimeStamp = startTimeStamp + day * 24 * 60 * 60 * 1000;
                String startDate = DateHelper.date2String(new Date(startTimeStamp),"yyyy-MM-dd HH:mm:ss");
                String endDate = DateHelper.date2String(new Date(endTimeStamp),"yyyy-MM-dd HH:mm:ss");
                call = getGameServer().getConnection().prepareCall(sql);
                call.setString(1, one.get("userId"));
                call.setString(2, startDate);
                call.setString(3, endDate);
                rs = call.executeQuery();
                if(rs.next()){
                    one.put("密卷充值时间",rs.getString("createTime"));
                    Map<String,String> userInfo = getUserInfo2(one.get("userId"));
                    one.put("用户注册时间",userInfo.get("registerTime"));
                    one.put("最后登录时间",userInfo.get("lastLoginTime"));
                    Map<String,String> lastOrder = getLastRechargeOrder(one.get("userId"));
                    one.put("最后充值时间",lastOrder.get("createTime"));
                    dataList.add(one);
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
            return dataList;
        }
    }

    private Map<String, String> getLastRechargeOrder(String userId) {
        Map<String, String> data = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_recharge_detail " +
                " WHERE userId = ? AND (orderStatus = 1 or orderStatus = 2)" +
                " ORDER BY createTime DESC LIMIT 1";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            call.setString(1, userId);
            rs = call.executeQuery();
            if(rs.next()){
                data.put("orderNo",rs.getString("orderNo"));
                data.put("createTime",rs.getString("createTime"));
                return data;
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return data;
    }

}
